﻿using System;

namespace ch09.binary.interpreter
{
    using WasmVal = Object;

    public static class Native
    {
        public static WasmVal[] PrintChar(WasmVal[] args)
        {
            Console.Write(Convert.ToChar(args[0]));
            return null;
        }

        public static WasmVal[] AssertTrue(WasmVal[] args)
        {
            AssertEq((int) args[0], 1);
            return null;
        }

        public static WasmVal[] AssertFalse(WasmVal[] args)
        {
            AssertEq((int) args[0], 0);
            return null;
        }

        public static WasmVal[] AssertEqI32(WasmVal[] args)
        {
            AssertEq((int) args[0], (int) args[1]);
            return null;
        }

        public static WasmVal[] AssertEqI64(WasmVal[] args)
        {
            AssertEq((long) args[0], (long) args[1]);
            return null;
        }

        public static WasmVal[] AssertEqF32(WasmVal[] args)
        {
            AssertEq((float) args[0], (float) args[1]);
            return null;
        }

        public static WasmVal[] AssertEqF64(WasmVal[] args)
        {
            AssertEq((double) args[0], (double) args[1]);
            return null;
        }

        private static void AssertEq(WasmVal a, WasmVal b)
        {
            if (!a.Equals(b))
            {
                Panic.Run($"{a:G} != {b:G}");
            }
        }
    }
}